<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="time" >2019-06-06 18:40:52</div>
    <button id="start">启动</button>
    <button id="stop">停止</button>
    <script>
        /*4、使用定时器函数和Date对象实现，数字时钟（当前时间是：2019-06-06 18:40:52）*/
        window.onload = function () {
            var time = null;
            var start = document.getElementById("start");
            var stop = document.getElementById("stop");
            start.onclick = function () {
                time = window.setInterval(nowTime,1000);
            }
            stop.onclick = function () {
                window.clearInterval(time);
            }
        }
        function nowTime() {
            var clock = document.getElementById("time");
            var date = new Date();
            var yyyy = date.getFullYear();
            var month = date.getMonth() + 1;
            var Month;
            if (month < 10){
                Month = "0" + month;
            }
            var day = date.getDate();
            var Day;
            if (day < 10){
                Day = "0" + day;
            }
            var Hour;
            var hour = date.getHours();
            if (hour < 10){
                Hour = "0" + hour;
            }else{
                Hour = hour;
            }
            var Minutes;
            var minutes = date.getMinutes();
            if (minutes < 10){
                Minutes = "0" + minutes;
            }else{
                Minutes = minutes;
            }
            var Second;
            var second = date.getSeconds();
            if (second < 10){
                Second = "0" + second;
            }else{
                Second = second;
            }
            var string = yyyy + "-" + Month + "-" + Day + " " + Hour + ":" + Minutes + ":" + Second;
            clock.innerHTML = string;
        }
    </script>
</body>
</html>